package cn.sunshuai.controller;

import cn.sunshuai.domain.Movie;
import cn.sunshuai.exception.BusinessException;
import cn.sunshuai.feignclient.MovieFeignClient;
import cn.sunshuai.service.IUserService;
import cn.sunshuai.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private MovieFeignClient movieFeignClient;

    @Autowired
    private IUserService userService;

    @GetMapping
    public List<Movie> findAll(){
        return movieFeignClient.findAll();
    }

    @GetMapping("/{id}")
    public AjaxResult movieBuy(@PathVariable("id")Long id){
        try {
            userService.movieBuy(id);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙,请稍后再试!!");
        }
    }

    @GetMapping("/user/{id}")
    public AjaxResult movieSee(@PathVariable("id")Long id){
        try {
            userService.movieSee(id);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙,请稍后再试!!");
        }
    }
}
